{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "56faf599",
   "metadata": {},
   "source": [
    "# 购物车小程序练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4104c72",
   "metadata": {},
   "source": [
    "> 1.启动小程序时，让用户输入工资，然后打印商品列表\n",
    "\n",
    "> 2.允许用户根据商品编号购买商品\n",
    "\n",
    "> 3.用户选择商品时，检测余额是否足够，够就直接扣款，不够则提醒\n",
    "\n",
    "> 4.可以随时退出，退出时，打印已经购买商品列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "199d961a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d4d2595",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的薪水：20000\n",
      "[1, '机器学习', 80]\n",
      "[2, '机器学习实战', 90]\n",
      "[3, 'QT5', 60]\n",
      "[4, '统计学习方法', 40]\n",
      "[5, '集体智慧编程', 30]\n",
      "请输入您需购买的商品编号:5\n",
      "购买 集体智慧编程 成功:\n",
      "余额：  19970\n",
      "请输入您需购买的商品编号:5\n",
      "购买 集体智慧编程 成功:\n",
      "余额：  19940\n",
      "请输入您需购买的商品编号:6\n",
      "编号 6不存在\n",
      "请输入数字\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be8ef155",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的薪水：100\n",
      "[1, 'KFC', 80]\n",
      "[2, '麦当劳', 90]\n",
      "[3, '汉堡王', 60]\n",
      "[4, '必胜客', 40]\n",
      "[5, '华莱士', 30]\n",
      "请输入您需购买的商品编号:1\n",
      "购买 KFC 成功:\n",
      "余额：  20\n",
      "请输入您需购买的商品编号:2\n",
      "余额不足....\n"
     ]
    }
   ],
   "source": [
    "def get_shopNode(num):\n",
    "    for i in shop_list:\n",
    "        if num in i:\n",
    "            return shop_list.index(i),i.index(num)   \n",
    "    else:\n",
    "        print(\"编号 %d不存在\" %num)\n",
    "        return \"null\",\"null\"\n",
    "         \n",
    "shop_list = [[1,\"KFC\",80],\n",
    "             [2,\"麦当劳\",90],\n",
    "             [3,\"汉堡王\",60],\n",
    "             [4,\"必胜客\",40],\n",
    "             [5,\"华莱士\",30]]\n",
    "have_shop = []\n",
    "break_flag = 0 \n",
    "while break_flag == 0:\n",
    "    try:        \n",
    "        input_sa = input(\"请输入您的薪水：\")\n",
    "        if input_sa == 'q':\n",
    "            break_flag = 1\n",
    "            break\n",
    "        else:\n",
    "            input_salary = int(input_sa)        \n",
    "    except:\n",
    "        print(\".........输入格式为数字.....\")\n",
    "        continue\n",
    "    for i in shop_list:\n",
    "        print(i)\n",
    "    while True:\n",
    "        try:\n",
    "            input_sN = input(\"请输入您需购买的商品编号:\")\n",
    "            if input_sN == 'q':\n",
    "                break_flag = 1\n",
    "                break\n",
    "            else:\n",
    "                input_shopNode = int(input_sN)\n",
    "            return_nodeA,return_nodeB = get_shopNode(input_shopNode)\n",
    "            if return_nodeA == \"null\" and return_nodeB == \"null\":\n",
    "                continue\n",
    "            price = shop_list[return_nodeA][return_nodeB+2]    \n",
    "            if input_salary >= price:\n",
    "                input_salary -= price\n",
    "                book_name = shop_list[return_nodeA][return_nodeB+1]\n",
    "                have_shop.append(book_name)\n",
    "                print(\"购买 %s 成功:\" %book_name)\n",
    "                print(\"余额： \",input_salary)  \n",
    "            else:\n",
    "                print(\"余额不足....\")                       \n",
    "        except:\n",
    "            print(\"请输入数字\")\n",
    "            continue\n",
    "    if break_flag == 1:\n",
    "        print(\"已购买商品： %s  余额：%d\" %(have_shop,input_salary))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78a75593",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
